{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}语音识别模型基准测试系统 - 测试项目{% endblock %}

{% block page_content %}
<div class="page-header">
    <a href="{{ url_for('.test_project') }}">返回测试项目主页</a>
    <h1>新增</h1>
</div>
<div class="col-md-4">
    <form action="#" method="post">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <input type="hidden" name="id" value="{{ id }}" id="id-input">
        <p>名称：<br>
            <input type="text" name="name" value="{{ name }}" required maxlength="20" id="name-input">
        </p>
        <p id="model-p">语音识别模型名称：<br>
        <input type="text" name="model_name" id="model_name" value="{{ model_name }}"><a class="btn btn-default" href="javascript:void(0)" onclick="select_model_name(1)">+</a></p>
        <div id="model-div"></div>
        <p id="fp-p">语音的前端处理器（+特征提取器）名称：<br>
        <input type="text" name="fp_name" id="fp_name" value="{{ fp_name }}"><a class="btn btn-default" href="javascript:void(0)" onclick="select_fp_name(1)">+</a><span id="fp-fe-type-error-msg" style="color:red"></span></p>
        <input type="hidden" value="{{ fp_type }}" id="fp_type">
        <div id="fp-div"></div>
        <p id="fe-p">语音的特征提取器名称：<br>
        <input type="text" name="fe_name" id="fe_name" value="{{ fe_name }}"><a class="btn btn-default" href="javascript:void(0)" onclick="select_fe_name(1)">+</a><a class="btn btn-default" href="javascript:void(0)" onclick="clear_fe_info();">清空特征提取器信息</a></p>
        <input type="hidden" value="{{ fe_type }}" id="fe_type">
        <div id="fe-div"></div>
        <p id="train-dp-p">模型的训练数据预处理器名称：<br>
        <input type="text" name="train_dp_name" id="train_dp_name" value="{{ train_dp_name }}"><a class="btn btn-default" href="javascript:void(0)" onclick="select_train_dp_name(1)">+</a></p>
        <div id="train-dp-div"></div>
        <p id="val-dp-p">模型的验证数据预处理器名称：<br>
            <input type="text" name="val_dp_name" id="val_dp_name" value="{{ val_dp_name }}"><a class="btn btn-default" href="javascript:void(0)" onclick="select_val_dp_name(1)">+</a></p>
            <div id="val-dp-div"></div>
            <p id="test-dp-p">模型的测试数据预处理器名称：<br>
                <input type="text" name="test_dp_name" id="test_dp_name" value="{{ test_dp_name }}"><a class="btn btn-default" href="javascript:void(0)" onclick="select_test_dp_name(1)">+</a></p>
                <div id="test-dp-div"></div>
                <p><label id="summary-error"></label></p>
        <p><button type="submit" id="submit-input">提交</button></p>
    </form>
</div>
<script type="text/javascript">
    function select_model_name(page){
        var div_html='';
        $.getJSON('/exec/model?page='+page,function(data){
            let models=data['models'];
            let page_range=data['page_range'];
            for(let i=0;i<models.length;i++){
                div_html+='<a href="/m/'+models[i][0]+'">'+models[i][1]+'</a> <button onclick="$(\'#model_name\').val(\''+models[i][1]+'\');" type="button">选择</button> ';
            }
            if(div_html.length==0){
                div_html='暂时没有数据';
            }
            div_html+='<br><ul class="pagination">';
            for(let i=0;i<page_range.length;i++){
                if(page==page_range[i]){
                    div_html+='<li class="active">'+page+'</li>';
                }else{
                    div_html+='<li><a href="javascript:void(0)" onclick="select_model_name('+page_range[i]+')">'+page_range[i]+'</a></li>';
                }
            }
            div_html+='</ul>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/model_add">添加新的语音识别模型</a>';
            $('#model-div').html(div_html);
        });
    };
    function select_fp_name(page){
        var div_html='';
        $.getJSON('/exec/fp?page='+page,function(data){
            let fps=data['fps'];
            let page_range=data['page_range'];
            for(let i=0;i<fps.length;i++){
                div_html+='<a href="/ff/'+fps[i][0]+'">'+fps[i][1]+'</a> <button onclick="$(\'#fp_name\').val(\''+fps[i][1]+'\');$(\'#fp_type\').val(\''+fps[i][2]+'\');" type="button">选择</button> ';
            }
            if(div_html.length==0){
                div_html='暂时没有数据';
            }
            div_html+='<br><ul class="pagination">';
            for(let i=0;i<page_range.length;i++){
                if(page==page_range[i]){
                    div_html+='<li class="active">'+page+'</li>';
                }else{
                    div_html+='<li><a href="javascript:void(0)" onclick="select_fp_name('+page_range[i]+')">'+page_range[i]+'</a></li>';
                }
            }
            div_html+='</ul>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/fpfe_add">添加新的语音的前端处理器和特征提取器</a>';
            $('#fp-div').html(div_html);
        });
    };
    function select_fe_name(page){
        var div_html='';
        $.getJSON('/exec/fe?page='+page,function(data){
            let fes=data['fes'];
            let page_range=data['page_range'];
            for(let i=0;i<fes.length;i++){
                div_html+='<a href="/ff/'+fes[i][0]+'">'+fes[i][1]+'</a> <button onclick="$(\'#fe_name\').val(\''+fes[i][1]+'\');$(\'fe_type\').val(\''+fes[i][2]+'\');" type="button">选择</button> ';
            }
            if(div_html.length==0){
                div_html='暂时没有数据';
            }
            div_html+='<br><ul class="pagination">';
            for(let i=0;i<page_range.length;i++){
                if(page==page_range[i]){
                    div_html+='<li class="active">'+page+'</li>';
                }else{
                    div_html+='<li><a href="javascript:void(0)" onclick="select_fe_name('+page_range[i]+')">'+page_range[i]+'</a></li>';
                }
            }
            div_html+='</ul>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/fpfe_add">添加新的语音的前端处理器和特征提取器</a>';
            $('#fe-div').html(div_html);
        });
    };
    function select_train_dp_name(page){
        var div_html='';
        $.getJSON('/exec/data_preprocessor?page='+page,function(data){
            let data_preprocessors=data['data_preprocessors'];
            let page_range=data['page_range'];
            for(let i=0;i<data_preprocessors.length;i++){
                div_html+='<a href="/dp/'+data_preprocessors[i][0]+'">'+data_preprocessors[i][1]+'</a> <button onclick="$(\'#train_dp_name\').val(\''+data_preprocessors[i][1]+'\');" type="button">选择</button> ';
            }
            if(div_html.length==0){
                div_html='暂时没有数据';
            }
            div_html+='<br><ul class="pagination">';
            for(let i=0;i<page_range.length;i++){
                if(page==page_range[i]){
                    div_html+='<li class="active">'+page+'</li>';
                }else{
                    div_html+='<li><a href="javascript:void(0)" onclick="select_train_data_preprocessor_name('+page_range[i]+')">'+page_range[i]+'</a></li>';
                }
            }
            div_html+='</ul>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/data_preprocessor_add">添加新的数据预处理器</a>';
            $('#train-dp-div').html(div_html);
        });
    };
    function select_val_dp_name(page){
        var div_html='';
        $.getJSON('/exec/data_preprocessor?page='+page,function(data){
            let data_preprocessors=data['data_preprocessors'];
            let page_range=data['page_range'];
            for(let i=0;i<data_preprocessors.length;i++){
                div_html+='<a href="/dp/'+data_preprocessors[i][0]+'">'+data_preprocessors[i][1]+'</a> <button onclick="$(\'#val_dp_name\').val(\''+data_preprocessors[i][1]+'\');" type="button">选择</button> ';
            }
            if(div_html.length==0){
                div_html='暂时没有数据';
            }
            div_html+='<br><ul class="pagination">';
            for(let i=0;i<page_range.length;i++){
                if(page==page_range[i]){
                    div_html+='<li class="active">'+page+'</li>';
                }else{
                    div_html+='<li><a href="javascript:void(0)" onclick="select_val_data_preprocessor_name('+page_range[i]+')">'+page_range[i]+'</a></li>';
                }
            }
            div_html+='</ul>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/data_preprocessor_add">添加新的数据预处理器</a>';
            $('#val-dp-div').html(div_html);
        });
    };
    function select_test_dp_name(page){
        var div_html='';
        $.getJSON('/exec/data_preprocessor?page='+page,function(data){
            let data_preprocessors=data['data_preprocessors'];
            let page_range=data['page_range'];
            for(let i=0;i<data_preprocessors.length;i++){
                div_html+='<a href="/dp/'+data_preprocessors[i][0]+'">'+data_preprocessors[i][1]+'</a> <button onclick="$(\'#test_dp_name\').val(\''+data_preprocessors[i][1]+'\');" type="button">选择</button> ';
            }
            if(div_html.length==0){
                div_html='暂时没有数据';
            }
            div_html+='<br><ul class="pagination">';
            for(let i=0;i<page_range.length;i++){
                if(page==page_range[i]){
                    div_html+='<li class="active">'+page+'</li>';
                }else{
                    div_html+='<li><a href="javascript:void(0)" onclick="select_test_data_preprocessor_name('+page_range[i]+')">'+page_range[i]+'</a></li>';
                }
            }
            div_html+='</ul>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/data_preprocessor_add">添加新的数据预处理器</a>';
            $('#test-dp-div').html(div_html);
        });
    };
    function clear_fe_info(){
        $('#fe_name').val('');
        $('#fe_type').val('-1');
    }
    function check_test_project(){
        if(!(($('#fp_type').val()=='0'&&$('#fe_type').val()=='1')||($('#fp_type').val()=='2'&&$('#fe_type').val()=='-1'))){
            $('#fp-fe-type-error-msg').text('语音的前端处理器和特征提取器的搭配是非法的，只能是一个前端处理器+一个特征提取器');
            $('#summary-error').text('语音的前端处理器和特征提取器的搭配是非法的，只能是一个前端处理器+一个特征提取器');
            return false;
        }
        var name_pass;
        $.ajaxSetup({async:false});
        $.getJSON('/exec/check_test_project_name?name='+$('#name-input').val()+'&id='+$('#id-input').val(),function(data){
            name_pass=data['res'];
        });
        if(! name_pass){
            $('#name-err-msg').text('测试项目名称与已存在的项目名称重复，请更名');
            $('#summary-error').text('测试项目名称与已存在的项目名称重复，请更名');
        }
        return name_pass;
    };
    $('#submit-input').click(check_test_project);
</script>
{% endblock %}